import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.SwingConstants;


public class ResetPasswordPanel extends JPanel {
	private final JTextField txtUsername;

	private final JTextField txtAdminUsername;
	
	private final JTextField adminPassword;
	private JTextField txtPassword;

	User currentUser;
	private JLabel lblResults;
	
	private class buttonListener implements ActionListener{

		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(txtAdminUsername.getText().equals(currentUser.username) 
					&& adminPassword.getText().equals(currentUser.password)){
				
				ArrayList<User> result = UserDatabase.searchUser(txtUsername.getText(), UserDatabase.USERNAME_SEARCH);
				User current = null;
				if(!result.isEmpty()){
					current = result.get(0);
				} 
				if(current != null){
					UserDatabase.removeUser(current.username, UserDatabase.DOCTOR);
					UserDatabase.removeUser(current.username, UserDatabase.PATIENT);
					UserDatabase.removeUser(current.username, UserDatabase.SYSTEM_ADMIN);
					UserDatabase.removeUser(current.username, UserDatabase.NURSE);
					current.password = txtPassword.getText();
					
					if(current.usertype.equals("doctor")){
						UserDatabase.addUser(current, UserDatabase.DOCTOR);
					}
					else if(current.usertype.equals("patient")){
						UserDatabase.addUser(current, UserDatabase.PATIENT);
					}
					else if(current.usertype.equals("nurse")){
						UserDatabase.addUser(current, UserDatabase.NURSE);
					}
					else if(current.usertype.equals("admin")){
						UserDatabase.addUser(current, UserDatabase.SYSTEM_ADMIN);
					}
					
				}
				
				DataSaveLoad.save();
				lblResults.setText("Password Reset");
			}
			else{
				lblResults.setText("Wrong admin credentials");
			}
			
		}

		
		
		
	}
	
	public ResetPasswordPanel() {
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		JLabel lblResetUserPassword = new JLabel("Reset User Password");
		springLayout.putConstraint(SpringLayout.NORTH, lblResetUserPassword, 10, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, 
				lblResetUserPassword, 125, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, lblResetUserPassword, 30, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, lblResetUserPassword, 315, SpringLayout.WEST, this);
		lblResetUserPassword.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(lblResetUserPassword);
		
		txtUsername = new JTextField();
		add(txtUsername);
		txtUsername.setColumns(10);
		
		JLabel label_1 = new JLabel("System Adminstator Authorization:");
		springLayout.putConstraint(SpringLayout.WEST, label_1, 64, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, label_1, 370, SpringLayout.WEST, this);
		label_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(label_1);
		
		txtAdminUsername = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtAdminUsername, 176, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.SOUTH, label_1, -6, SpringLayout.NORTH, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.WEST, 
				txtAdminUsername, 125, SpringLayout.WEST, this);
		add(txtAdminUsername);
		txtAdminUsername.setColumns(10);
		
		adminPassword = new JTextField("");
		springLayout.putConstraint(SpringLayout.NORTH, 
				adminPassword, 19, SpringLayout.SOUTH, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.EAST, 
				txtAdminUsername, 0, SpringLayout.EAST, adminPassword);
		springLayout.putConstraint(SpringLayout.WEST, 
				adminPassword, 125, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				adminPassword, 285, SpringLayout.WEST, this);
		add(adminPassword);
		adminPassword.setColumns(10);
		
		JButton btnNewButton = new JButton("Reset");
		springLayout.putConstraint(SpringLayout.NORTH, 
				btnNewButton, -1, SpringLayout.NORTH, adminPassword);
		springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 34, SpringLayout.EAST, adminPassword);
		add(btnNewButton);
		btnNewButton.addActionListener(new buttonListener());
		
		JLabel lblEnterTheUsername = new JLabel("Enter the Username of" +
				" the User you want to reset " +
				"the password for,\r\n");
		springLayout.putConstraint(SpringLayout.NORTH, 
				lblEnterTheUsername, 36, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, 
				lblEnterTheUsername, 64, SpringLayout.WEST, this);
		add(lblEnterTheUsername);
		
		txtPassword = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtPassword, 6, SpringLayout.SOUTH, txtUsername);
		springLayout.putConstraint(SpringLayout.EAST, txtPassword, 0, SpringLayout.EAST, txtUsername);
		add(txtPassword);
		txtPassword.setColumns(10);
		
		JLabel lblAndTheirNew = new JLabel("and their new password.");
		springLayout.putConstraint(SpringLayout.WEST, lblAndTheirNew, 64, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, txtUsername, 6, SpringLayout.SOUTH, lblAndTheirNew);
		springLayout.putConstraint(SpringLayout.NORTH,
				lblAndTheirNew, 8, SpringLayout.SOUTH, lblEnterTheUsername);
		add(lblAndTheirNew);
		
		JLabel lblUsername = new JLabel("Username:");
		springLayout.putConstraint(SpringLayout.WEST, txtUsername, 21, SpringLayout.EAST, lblUsername);
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 9, SpringLayout.SOUTH, lblAndTheirNew);
		springLayout.putConstraint(SpringLayout.WEST, lblUsername, 80, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblUsername, 141, SpringLayout.WEST, this);
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 12, SpringLayout.SOUTH, lblUsername);
		springLayout.putConstraint(SpringLayout.WEST, lblPassword, 84, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblPassword, 141, SpringLayout.WEST, this);
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblPassword);
		
		JLabel lblAdminPass = new JLabel("Admin Password:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAdminPass, 3, SpringLayout.NORTH, adminPassword);
		springLayout.putConstraint(SpringLayout.EAST, lblAdminPass, -6, SpringLayout.WEST, adminPassword);
		lblAdminPass.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAdminPass);
		
		JLabel lblAdminUsername = new JLabel("Admin Username:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAdminUsername, 3, SpringLayout.NORTH, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.EAST, lblAdminUsername, -6, SpringLayout.WEST, txtAdminUsername);
		lblAdminUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAdminUsername);
		
		lblResults = new JLabel("");
		springLayout.putConstraint(SpringLayout.WEST, lblResults, -160, SpringLayout.EAST, txtAdminUsername);
		lblResults.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.SOUTH, lblResults, -10, SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, lblResults, 0, SpringLayout.EAST, txtAdminUsername);
		lblResults.setForeground(Color.RED);
		lblResults.setFont(new Font("Tahoma", Font.ITALIC, 11));
		add(lblResults);
	}
	public JLabel getLblResults() {
		return lblResults;
	}
	public JTextField getTxtAdminUsername() {
		return txtAdminUsername;
	}
	public JTextField getTextField() {
		return adminPassword;
	}
	public JTextField getTxtPassword() {
		return txtPassword;
	}
	public JTextField getTxtUsername() {
		return txtUsername;
	}
	
	public String toString(){
		return "passwordReset";
	}
}
